using System.IO;
using UnityEditor;
using UnityEngine;

namespace EntityKit.Editor
{
    /// <summary>
    /// 截图工具类，提供多种分辨率的截图功能
    /// </summary>
    public static class ScreenshotUtility
    {
        /// <summary>
        /// 默认分辨率截图
        /// </summary>
        public static void TakeDefaultScreenshot()
        {
            var filePath = Application.dataPath + "/../Captures";
            if (!Directory.Exists(filePath))
                Directory.CreateDirectory(filePath);
            var files = Directory.GetDirectories(Application.dataPath + "/../Captures");
            string fileName = $"{Application.dataPath}/../Captures/{files.Length + 1}.png";
            ScreenCapture.CaptureScreenshot(fileName);
        }
        
        /// <summary>
        /// 4K主相机截图 (3840x2160)
        /// </summary>
        public static void TakeMainCameraShot4K()
        {
            TakeMainCameraShot(3840, 2160, "4K");
        }
        
        /// <summary>
        /// 8K主相机截图 (7680x4320)
        /// </summary>
        public static void TakeMainCameraShot8K()
        {
            TakeMainCameraShot(7680, 4320, "8K");
        }
        
        /// <summary>
        /// 超高清主相机截图 (15360x8640)
        /// </summary>
        public static void TakeMainCameraShotUHD()
        {
            TakeMainCameraShot(15360, 8640, "UHD");
        }
        
        /// <summary>
        /// 使用主相机截取指定分辨率的图片
        /// </summary>
        /// <param name="width">截图宽度</param>
        /// <param name="height">截图高度</param>
        /// <param name="resolutionName">分辨率名称</param>
        private static void TakeMainCameraShot(int width, int height, string resolutionName)
        {
            Camera mainCamera = Camera.main;
            if (mainCamera == null)
            {
                Debug.LogError("未找到主相机");
                EditorUtility.DisplayDialog("错误", "未找到主相机", "确定");
                return;
            }

            // 保存原始设置
            RenderTexture originalRT = mainCamera.targetTexture;
            int originalWidth = Screen.width;
            int originalHeight = Screen.height;
            RenderTexture originalActiveRT = RenderTexture.active;

            try
            {
                // 创建新的RenderTexture
                RenderTexture renderTexture = new RenderTexture(width, height, 24);
                mainCamera.targetTexture = renderTexture;
                RenderTexture.active = renderTexture;

                // 渲染相机视图
                mainCamera.Render();

                // 从RenderTexture创建Texture2D
                Texture2D screenshot = new Texture2D(width, height, TextureFormat.RGB24, false);
                screenshot.ReadPixels(new Rect(0, 0, width, height), 0, 0);
                screenshot.Apply();

                // 保存截图
                var filePath = Application.dataPath + "/../Captures";
                if (!Directory.Exists(filePath))
                    Directory.CreateDirectory(filePath);
                
                string fileName = $"{filePath}/{System.DateTime.Now:yyyyMMdd_HHmmss}_{resolutionName}_{width}x{height}.png";
                File.WriteAllBytes(fileName, screenshot.EncodeToPNG());

                // 清理资源
                RenderTexture.active = originalActiveRT;
                mainCamera.targetTexture = originalRT;
                Object.DestroyImmediate(renderTexture);
                Object.DestroyImmediate(screenshot);

                // 刷新资源数据库以显示新文件
                AssetDatabase.Refresh();
                
                Debug.Log($"截图已保存: {fileName}");
            }
            catch (System.Exception e)
            {
                // 恢复原始设置
                RenderTexture.active = originalActiveRT;
                mainCamera.targetTexture = originalRT;
                
                Debug.LogError($"截图失败: {e.Message}");
                EditorUtility.DisplayDialog("错误", $"截图失败: {e.Message}", "确定");
            }
        }
    }
}